// page/detail/pages/crowd/crowd.js
const utils = require('../../../../utils/util.js');
let myRequest = require('../../../../common/script/myRequest.js');
let api = require('../../../../common/script/api.js');
let login = require('../../../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, // 页码
    pageSize: 10,
    title: '',
    dataList: [],
    crowdId: '',
    isEnd: false,
    isCan: 1,
    crowd: {}
  },
  //领资
  btn() {
    const that = this;
    wx.showModal({
      content: '是否领取群友资格码？',
      success(res) {
        if (res.confirm) {
          let data = {
            token: wx.getStorageSync('token'),
            id: that.data.crowdId,
          }
          wx.showLoading({
            title: '加载中...',
          })
          myRequest.EasyHttp.post(api.reqUrls.crowdHandle, data).then(res => {
            wx.hideLoading();
            if (res.data.status == 1) {
              wx.showToast({
                title: res.data.msg,
                icon: 'success',
                duration: 2000
              })
              setTimeout(function () {
                wx.redirectTo({
                  url: `/package/pages/groupFriend/groupFriend`,
                })
              }, 2000)

            } else {
              wx.showModal({
                content: res.data.msg,
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#ff5151',
              })
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  getData() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
      id: that.data.crowdId,
      page: that.data.page,
      limit: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.crowdPage, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          dataList: res.data.result.details,
          isCan: res.data.result.crowd.is_can,
          crowd: res.data.result.crowd,
          title: res.data.result.crowd.title + '(' + res.data.result.store.store_name + ')',
        })

      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 999)
    const that = this;
    let sceneObj = utils.parseQueryString(options.scene)
    that.setData({
      crowdId: sceneObj.id,
    })
    login.checkLogin().then(res => {
      that.getData()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getData()
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      let page = that.data.page;
      page++;
      let data = {
        token: wx.getStorageSync('token'),
        id: that.data.crowdId,
        page,
        limit: that.data.pageSize,
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.crowdPage, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          if (res.data.result.details.length == 0) {
            that.setData({
              isEnd: true
            })
          } else {
            let dataList = [...that.data.dataList, ...res.data.result.details]
            that.setData({
              page,
              dataList
            })
          }
        }
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})